#!/bin/bash
#zhushi:Http status number
#author_Name:Weiyi

resettem=$(tput sgr0)
read -t 30 -p "Please,Input access Httpd log pash:" logfile

#Check http status Functions
Check_http_status(){
	http_status_codes=$(cat $logfile |grep -ioE 'HTTP\/1\.[0|1]\"[[:blank:]][0-9]{3}' |awk -F "[ ]+" '{
		if ( $2>100 && $2<200 )
			{i++} 
		else if ( $2>200 && $2<300 )
			{j++}
		else if ( $2>300 && $2<400 )
			{k++}
		else if ( $2>400 && $2<500 )
			{n++}
		else if ( $2>500 )
			{p++}
		}END{
			print i?i:0,j?j:0,k?k:0,n?n:0,p?p:0,i+j+k+n+p
		}')
	echo -e '\E[33m' "The Number of http status[100+]:" $resettem ${http_status_codes[0]}
	echo -e '\E[33m' "The Number of http status[200+]:" $resettem ${http_status_codes[1]}
	echo -e '\E[33m' "The Number of http status[300+]:" $resettem ${http_status_codes[2]}
	echo -e '\E[33m' "The Number of http status[400+]:" $resettem ${http_status_codes[3]}
	echo -e '\E[33m' "The Number of http status[500+]:" $resettem ${http_status_codes[4]}
	echo -e '\E[33m' "All Request numbers:" $resettem ${http_status_codes[5]}
}

#Check http Code 404/403 Functions
Check_http_code()
{
	http_code=$(cat $logfile |grep -ioE 'HTTP\/1\.[0|1]\"[[:blank:]][0-9]{3}' |awk -v total=0 -F '[ ]+' '{
		if ( $2 != "" )
			{code[$2]++;total++}
		else
			{exit}
	}END{
		print code[404]?code[404]:0,code[403]?code[403]:0,total
	}')
	echo -e '\E[33m' "The Number of http status[404]:" $resettem ${http_status_codes[0]}
	echo -e '\E[33m' "The Number of http status[403]:" $resettem ${http_status_codes[1]}
	echo -e '\E[33m' "All Request numbers:" $resettem ${http_status_codes[2]}
}

#调用函数
Check_http_status

echo -e '\n'

Check_http_code

